package cn.edu.abc.wangzi.ui;
/*
 *  项目名：Wangzi
 *  包名：cn.edu.abc.wangzi.ui
 *  创建者：SKFsky
 *  创建时间：2017/8/26 22:13
 *  描述:   引导页面
 */

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;
import cn.edu.abc.wangzi.R;
import cn.edu.abc.wangzi.utils.UtilTools;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.L;

public class GuideActivity extends AppCompatActivity implements View.OnClickListener{

    private ViewPager mViewPager;
    private List<View> mList = new ArrayList<>();
    private View view1,view2,view3;
    private ImageView point1,point2,point3;
    private ImageView iv_back;
    private TextView   tv_view_1,tv_view_2,tv_view_3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        initView();
    }

    private void initView() {
        point1 = (ImageView) findViewById(R.id.point1);
        point2 = (ImageView) findViewById(R.id.point2);
        point3 = (ImageView) findViewById(R.id.point3);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);
        setPointImg(true, false, false);
        mViewPager = (ViewPager) findViewById(R.id.mViewPager);
        view1 = View.inflate(this,R.layout.view1,null);
        view2 = View.inflate(this,R.layout.view2,null);
        view3 = View.inflate(this,R.layout.view3,null);
        //进入主页点击事件
        view3.findViewById(R.id.btn_start).setOnClickListener(this);
        tv_view_1 = (TextView) view1.findViewById(R.id.tv_view_1);
        tv_view_2 = (TextView) view2.findViewById(R.id.tv_view_2);
        tv_view_3 = (TextView) view3.findViewById(R.id.tv_view_3);
        UtilTools.setFont(this,tv_view_1);
        UtilTools.setFont(this,tv_view_2);
        UtilTools.setFont(this,tv_view_3);
        mList.add(view1);
        mList.add(view2);
        mList.add(view3);
        mViewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view==object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ((ViewPager)container).addView(mList.get(position));
                return mList.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                ((ViewPager)container).removeView(mList.get(position));
               // super.destroyItem(container, position, object);
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                switch (position){
                    case 0:
                        setPointImg(true,false,false);
                        iv_back.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        setPointImg(false,true,false);
                        iv_back.setVisibility(View.VISIBLE);
                        break;
                    case 2:
                        setPointImg(false,false,true);
                        iv_back.setVisibility(View.GONE);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_start:
            case R.id.iv_back:
                startActivity(new Intent(this, LoginActivity.class));
                finish();
                break;
        }
    }

    //设置小圆点的选中效果
    private void setPointImg(boolean isCheck1,boolean isCheck2,boolean isCheck3){
        if (isCheck1){
            point1.setBackgroundResource(R.drawable.point_on);
        }else{
            point1.setBackgroundResource(R.drawable.point_off);
        }

        if (isCheck2){
            point2.setBackgroundResource(R.drawable.point_on);
        }else{
            point2.setBackgroundResource(R.drawable.point_off);
        }

        if (isCheck3){
            point3.setBackgroundResource(R.drawable.point_on);
        }else{
            point3.setBackgroundResource(R.drawable.point_off);
        }
    }
}
